<?php

namespace Common\Model;

use Think\Model;

class AdvModel extends Model
{
    protected $tableName = 'adv';

    /*  展示数据  */
    public function getAdvList($name, $path)
    {


        $now_theme = modC('NOW_THEME', 'default', 'Theme');

        $advPos = D('Common/AdvPos')->getInfo($name, $path); //找到当前调用的广告位
        if ($advPos['theme'] != 'all' && !in_array($now_theme, explode(',', $advPos['theme']))) {
            return null;
        }

        $advMap['pos_id'] = $advPos['id'];
        $advMap['status'] = 1;
        $advMap['start_time']=array('lt',time());
        $advMap['end_time']=array('gt',time());
        if ($advPos['type'] == 2) {
            //todo 轮播兼容
           /* $advs = $this->where($advMap)->order('level asc,id asc')->select();
            foreach ($advs as $key => $val) {
                if (intval($val['create_time']) != 0) {

                    if ($val['create_time'] > time()) {
                        unset($advs[$key]);
                        continue;
                    }
                }
                if (intval($val['end_time']) != 0) {
                    if ($val['end_time'] < time()) {
                        unset($advs[$key]);
                        continue;
                    }
                }
                $data[$key] = $val;
                $cover = M('picture')->find($val['advspic']);
                $data[$key]['path'] = $cover['path'];
            }*/
        } else {
            $data = $this->where($advMap)->order('sort desc,id asc')->select();
        }

        foreach($data as &$v){
            $d=json_decode($v['data'],true);
            if(!empty($d)){
                $v=array_merge($d,$v);

            }
        }
        unset($v);
        return $data;
    }

    /*——————————————————分隔线————————————————*/


    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }

    /**
     * 新增或更新一个文档
     * @return boolean fasle 失败 ， int  成功 返回完整的数据
     */
    public function update()
    {
        $_POST['theme'] = $_POST['theme'] ? $_POST['theme'] : 'all';
        /* 获取数据对象 */
        $data = $this->create();
        if (empty($data)) {
            return false;
        }
        /* 添加或新增基础内容 */
        if (empty($data['id'])) { //新增数据
            $id = $this->add(); //添加基础内容
            if (!$id) {
                $this->error = '新增广告内容出错！';
                return false;
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容

            $this->clearCacheById($data['id']);

            if (false === $status) {
                $this->error = '更新广告内容出错！';
                return false;
            }
        }

        //内容添加或更新完成
        return $data;

    }

    /* 禁用 */
    public function forbidden($id)
    {
        $this->clearCacheById($id);
        return $this->save(array('id' => $id, 'status' => '0'));
    }

    /* 启用 */
    public function off($id)
    {
        $this->clearCacheById($id);
        return $this->save(array('id' => $id, 'status' => '1'));
    }

    /* 删除 */
    public function del($id)
    {
        $this->clearCacheById($id);
        return $this->delete($id);
    }

    /* 获取编辑数据 */
    public function detail($id)
    {
        $data = $this->find($id);
        return $data;
    }

    public function clearCacheById($id)
    {
        $info = $this->detail($id);
        S('advertising_by_pos_' . $info['pos'], null);
    }


}